﻿module Claims

open IntelliFactory.WebSharper
open IntelliFactory.WebSharper.Html
open IntelliFactory.WebSharper.JQuery
open IntelliFactory.WebSharper.JQuery.Mobile
open System

type Location={Lat:float; Lon:float}

type Claim = 
    {Id:string; PolicyId:string; PolicyType:string;
     Description:string; Location:Location; Status:string}

let mutable claimCache = []

[<Rpc>]
let saveClaim (c:Claim) =
    let nextId =
        match claimCache with
        | [] -> 4
        | cc ->
            (cc
            |> List.map (fun c->c.Id |> int)
            |> List.max) + 1
            
    let effectivePolicy = Policies.getPolicies()|>List.find (fun p-> p.Id = c.PolicyId)
    let newClaim = {c with Id=string nextId; PolicyType = effectivePolicy.Type}
    claimCache <- newClaim::claimCache |> Seq.truncate 10 |> Seq.toList
    ()

[<Rpc>]
let getClaims() =
    claimCache
    @
    ([
        "1", "1111", "Automotive", "Windshield cracked", 42.313735, -83.214827, "open"
        "2", "2222", "Home", "Wind / storm damage" , 42.388726, -83.170280, "closed"
        "3", "3333", "Flood", "Basement flooded" , 42.388726, -83.170280, "closed"
    ]
    |> List.map (fun (i,p,t,d,lt,ln,s) ->  
        {Id=i; PolicyId=p; PolicyType=t; Description=d; Location={Lat=lt;Lon=ln};Status=s}
        ))

[<Rpc>]
let getClaimsAsync() = async {return getClaims()}

[<JavaScript>] 
let ClaimPageContent() = JQuery.Of("#claims").Children(":jqmData(role=content)")

[<JavaScript>]
let MapUrl loc id = 
    let href =
        match loc with
        | {Lon=0.; Lat=0.} -> ""
        | _ -> "http://bing.com/maps/default.aspx?sp=point." + loc.Lat.ToString() + "_" + loc.Lon.ToString() + "_Claim%20" + id
    match href with
    | "" -> [Text ""]
    | h -> [A [HRef h; Attr.Target "_blank"] -<[Text "Map"]]


[<JavaScript>]
let ClaimsPageDetailContent claim =
    let s1 = "font-weight:bold; font-size:smaller; background:#D6EBFF; padding:5px"
    let s2 = "font-weight:bold; font-size:smaller; background:#E0FFFF; padding:5px"
    Div[] -< [//need a wrapper div
        Div [] -< [
            Div[Attr.Style s1; Attr.Class "ui-grid-a"] -< [
                Div[Attr.Class "ui-block-a"] -<  [Text "Claim ID"]
                Div[Attr.Class "ui-block-b"] -<  [Text claim.Id]
            ]
            Div[Attr.Style s2; Attr.Class "ui-grid-a"] -< [
                Div[Attr.Class "ui-block-a"] -<  [Text "Type"]
                Div[Attr.Class "ui-block-b"] -<  [Text claim.PolicyType]
            ]
            Div[Attr.Style s1; Attr.Class "ui-grid-a"] -< [
                Div[Attr.Class "ui-block-a"] -<  [Text "Description"]
                Div[Attr.Class "ui-block-b"] -<  [Text claim.Description]
            ]
            Div[Attr.Style s2; Attr.Class "ui-grid-a"] -< [
                Div[Attr.Class "ui-block-a"] -<  [Text "Location"]
                Div[Attr.Class "ui-block-b"] -<  (MapUrl claim.Location claim.Id)
            ]
            Div[Attr.Style s1; Attr.Class "ui-grid-a"] -< [
                Div[Attr.Class "ui-block-a"] -<  [Text "Status"]
                Div[Attr.Class "ui-block-b"] -<  [Text claim.Status]
            ]
        ]
    ]

[<JavaScript>]
let ClaimsDetailPage () = 
    
    let header =
        Div [HTML5.Attr.Data "role" "header"; HTML5.Attr.Data "theme" "b"] -< [

                SiteCommon.homeButton()

                H1 [Text "Claim Detail"]

                A [ HRef "#"; HTML5.Attr.Data "role" "button"; 
                    HTML5.Attr.Data "icon" "back"
                    HTML5.Attr.Data "inline" "true"
                ] -< [Text "Back"]
                |>! OnClick (fun _ _ -> 
                    JQuery.Of("#claims") 
                    |> Nav.transitionWith Nav.SlideLeft)
            ]

    let content =
        Div [HTML5.Attr.Data "role" "content"] -< [

            ]

    let page = 
        Div [Id "claimDetail"; HTML5.Attr.Data "role" "page"] -< [
            header
            content
            ]
    page

module ClaimController =
    //for injecting dynamic content into a page. see:
    //http://jquerymobile.com/test/docs/pages/page-dynamic.html
    [<JavaScript>]
    let ViewDetail claim =
        let page = JQuery.Of("#claimDetail")
        let contentSection = page.Children(":jqmData(role=content)")
        let content = ClaimsPageDetailContent claim
        contentSection.Html(content.Html) |> ignore
        page |> Nav.transitionWith Nav.SlideRight

[<JavaScript>]
let LoadClaims() =
    let s1 = "font-size:smaller"
    async {
        let! claims = getClaimsAsync()
        let children = 
            UL[HTML5.Attr.Data "role" "listview"] -<
                    [for p in claims do
                        yield LI [] -< [
                            A[] -< [
                                Div[Text "Claim Id: "; Text p.Id]
                                Div[Attr.Style s1] -< [Text "Policy: #"; Text p.PolicyId; Text "   ["; Text p.PolicyType; Text "]"]
                            ]
                            |>! OnClick (fun _ _ -> ClaimController.ViewDetail p)
                        ]
                    ]
        let jq =ClaimPageContent().Html("")
        jq.Get(0).InsertBefore(children.Dom,null) |> ignore
        jq.Trigger("create") |> ignore
    }

[<JavaScript>]
let ClaimsPage () = 
    
    let header =
        Div [HTML5.Attr.Data "role" "header"; HTML5.Attr.Data "theme" "b"] -< [
                
                SiteCommon.homeButton()
                   
                H1 [Text "Claims"]

                A [ HTML5.Attr.Data "role" "button"; 
                    HTML5.Attr.Data "icon" "plus"
                    HTML5.Attr.Data "inline" "true"
                ] -< [Text "New"]
                |>! OnClick (fun _ _ ->  
                    JQuery.Of("#newClaim") |> Nav.transitionWith Nav.SlideRight
                    )
            ]

    let content = Div[HTML5.Attr.Data "role" "content"]
        
    let page = 
        Div [Id "claims"; HTML5.Attr.Data "role" "page"] -< [
            header
            content
            SiteCommon.footer MainPages.Claims
            ]
        |>! OnBeforeShow(fun _ -> ClaimPageContent() |> ShowBusy)
        |>! OnShow(fun _ -> LoadClaims() |> Async.Start)
    page
